package inits

import (
	"context"
	"github.com/go-redis/redis/v8"
	"gorm.io/gorm/logger"
	"log"
	"srv-server/basic/global"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func InitAll() {
	MysqlInit()
	RedisInit()
}

func RedisInit() {
	global.Rdb = redis.NewClient(&redis.Options{
		Addr:     "14.103.143.155:6379",
		Password: "ae18a618f2bec0834cd76a6bb4c4aa36", // no password set
		DB:       0,                                  // use default DB
	})
	result, err := global.Rdb.Ping(context.Background()).Result()
	log.Println(result)
	if err != nil {
		panic(err)
	}
}

func MysqlInit() {
	var err error
	dsn := "root:ae18a618f2bec0834cd76a6bb4c4aa36@tcp(14.103.143.155:3306)/zk3?charset=utf8mb4&parseTime=True&loc=Local"
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic("数据库连接失败")
	} else {
		log.Println("数据库连接成功")
	}

}
